// pages/ehaes/index.js
import * as echarts from '../../ec-canvas/echarts';


Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    ec: {
      lazyLoad: true,
    },
    objectArray: [
      {id: 5, unique: 'unique_5'},
      {id: 4, unique: 'unique_4'},
      {id: 3, unique: 'unique_3'},
      {id: 2, unique: 'unique_2'},
      {id: 1, unique: 'unique_1'},
      {id: 0, unique: 'unique_0'},
    ],
    numberArray: [1, 2, 3, 4]
  },
  switch: function(e) {
    // 随机两个位置得调换
    const length = this.data.objectArray.length
    for (let i = 0; i < length; ++i) {
      const x = Math.floor(Math.random() * length)
      const y = Math.floor(Math.random() * length)
      const temp = this.data.objectArray[x]
      this.data.objectArray[x] = this.data.objectArray[y]
      this.data.objectArray[y] = temp
    }
    this.setData({
      objectArray: this.data.objectArray
    })
  },
  addToFront: function(e) {
    const length = this.data.objectArray.length
    console.log(length)
    // return;
    this.data.objectArray = [{id: length, unique: 'unique_' + length}].concat(this.data.objectArray)
    this.setData({
      objectArray: this.data.objectArray
    })
  },
   addNumberToFront: function(e){
    this.data.numberArray = [ this.data.numberArray.length + 1 ].concat(this.data.numberArray)
    this.setData({
      numberArray: this.data.numberArray
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.ecComponent =this.selectComponent('#mychart');
    this.getPriceByMonth()
    // this.
  },
  /**设置图表映射 */

  getPriceByMonth() {
    var that = this;
    let data=[
      {category:'1231',price:'57'},
      {category:'1231',price:'35'},
      {category:'1231',price:'25'},
      {category:'1231',price:'856'},

    ]
    var countPrice = 0;
    var xData = [];
    var yData = [];
    for (var i in data) {
      xData.push(data[i].category);
      yData.push(data[i].price);
    }
    //去调用初始化设置图表映射函数
    that.initChart(xData, yData);
    that.setData({
      xData,
      yData
    })
    // wx.request({
    //   url: app.globalData.appUrl + '/detail/getPriceByMonth',
    //   data: {
    //     //设置接口参数
    //     year: that.data.nowSelectYear.substring(0, 4),
    //     month: that.data.nowSelectMonth.substring(0, 2),
    //     openid: wx.getStorageSync('openid'),
    //     type: that.data.ieSelectStatus
    //   },
    //   success(res) {//成功回调
    //     var data = res.data;
    //     var countPrice = 0;
    //     var xData = [];
    //     var yData = [];
    //     for (var i in data) {
    //       xData.push(data[i].category);
    //       yData.push(data[i].price);
    //     }
    //     //去调用初始化设置图表映射函数
    //     that.initChart(xData, yData);
    //     that.setData({
    //       xData,
    //       yData
    //     })
    //   },
    //   fail(e) {}
    // })
  },
  
/**设置图表映射 */
initChart: function(xData, yData) {

  var that = this;
  var option = {
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        data: [150, 230, 224, 218, 135, 147, 260],
        type: 'line'
    }]
};
  // console.log(that.ecComponent)
  //echarts会继承父元素的宽高,所以我们一定要设置echarts组件父元素的高度。
  var myChart = that.ecComponent.init((canvas, width, height) => {
    const chart = echarts.init(canvas, null, {
      width: width,
      height: height,
    });
    //给echarts 设置数据及配置项（图表类型、数据量等）
    chart.setOption(option);
    return chart;
  });

},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
  